package com.populatton.config.swagger;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SwaggerConfig
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")
class SwaggerConfig  {

    @Value("${swagger.api.base.package}")
    private String basePackage;


    private Predicate<RequestHandler> getPredicate(final String basePackage){
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                if(declaringClass.getPackage().getName().startsWith(basePackage)){
                    // 排除
                    if (declaringClass == BasicErrorController.class){
                        return false;
                    }
                    // 被注解的类
                    if(declaringClass.isAnnotationPresent(RestController.class)){
                        return true;
                    }
                    // 被注解的方法
                    if(input.isAnnotatedWith(ResponseBody.class)){
                        return true;
                    }
                    return true;
                }
                return false;
            }
        };
        return predicate;
    }


    @Bean
    public Docket createRestApi() {


        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .apiInfo(apiInfo("系统接口文档"))
                .select()
                .paths(PathSelectors.any())
                .apis(getPredicate(basePackage))
                .build()
                ;
    }


    private ApiInfo apiInfo(String description) {
        return new ApiInfoBuilder()
                //大标题
                .title("九星文档")
                //详细描述
                .description(description)
                //版本
                .version("1.0.0")
                //条款服务网址
                .termsOfServiceUrl("https://dev.ninstarscf.com/")
                //作者信息
                .contact(contact())
                .build();
    }

    /**
     * description:设置作者信息
     * @author: Flying
     * @param: null
     * @return
     */
    private Contact contact() {
        return new Contact("shmily", "https://my.oschina.net/u/4161700/blog/", "scc1324@163.com");
    }


    }
